package com.turman.composebook.views

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument

@Composable
fun NavigationSampleView(modifier: Modifier = Modifier) {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "one") {
        composable("one") {
            One(navigator = navController, modifier = modifier)
        }
        composable("two") {
            Two(navigator = navController, modifier = modifier)
        }
        composable("three") {
            Three(navigator = navController, modifier = modifier)
        }
        composable(
            "four/{userName}",
            arguments = listOf(navArgument("userName") { type = NavType.StringType })
        ) {
            Four(
                navigator = navController,
                modifier = modifier,
                userName = it.arguments?.getString("userName") ?: ""
            )
        }
        composable(
            "five?userId={userId}&userName={userName}",
            arguments = listOf(navArgument("userId") {
                defaultValue = 1
                type = NavType.IntType
            }, navArgument("userName") {
                defaultValue = "James"
                type = NavType.StringType
            })
        ) {
            Five(
                navigator = navController,
                modifier = modifier,
                it.arguments?.getInt("userId") ?: 0,
                it.arguments?.getString("userName") ?: ""
            )
        }
    }
}

@Composable
fun One(navigator: NavController, modifier: Modifier = Modifier) {
    Column(modifier = modifier.fillMaxSize()) {
        Text(text = "Navigation Samples")
        Button(onClick = { navigator.navigate("two") }) {
            Text(text = "跳转到第二页")
        }
    }
}


@Composable
fun Two(navigator: NavController, modifier: Modifier = Modifier) {
    Column(modifier = modifier.fillMaxSize()) {
        Text(text = "second page")
        Button(onClick = { navigator.navigate("three") }) {
            Text(text = "跳转到第三页")
        }
        Button(onClick = {
            navigator.navigate("two")
        }) {
            Text(text = "打开本页")
        }

        //防止重复打开
        Button(onClick = {
            navigator.navigate("two") {
                launchSingleTop = true
            }
        }) {
            Text(text = "打开本页")
        }
    }
}

@Composable
fun Three(navigator: NavController, modifier: Modifier = Modifier) {
    Column(modifier = modifier.fillMaxSize()) {
        Text(text = "third page")
        Button(onClick = { navigator.navigate("four/buobao") }) {
            Text(text = "跳转到第四页")
        }
    }
}

@Composable
fun Four(navigator: NavController, modifier: Modifier = Modifier, userName: String) {
    Column(modifier = modifier.fillMaxSize()) {
        Text(text = "fourth page")
        Text(text = "userName:$userName")
        Button(onClick = { navigator.navigate("five") }) {
            Text(text = "跳转到第五页")
        }
    }
}

@Composable
fun Five(navigator: NavController, modifier: Modifier = Modifier, userId: Int, userName: String) {
    Column(modifier = modifier.fillMaxSize()) {
        Text(text = "fifth page")
        Text(text = "userId:$userId")
        Text(text = "name:$userName")
        Button(onClick = {
            //返回到首页，这里注意popUpTo的调用作用
            navigator.navigate("one") {
                popUpTo("one") { inclusive = true }
            }
        }) {
            Text(text = "返回首页")
        }
    }
}